package org.kk.tree;
import org.kk.Common.TreeNode;
/**
 * 判断二叉树A中是否包含子树B
 * @author kk
 * @Date 2021/1/23
 */
public class DoesTreeHave {
    /**
     * 判断是否包含子树
     */
    public static boolean hasSubTree(TreeNode A, TreeNode B){
        return (A != null && B != null) && (helper(A, B) || hasSubTree(A.left, B) || hasSubTree(A.right, B));
    }
    private static boolean helper(TreeNode A, TreeNode B){
        if(B == null) return true;
        if(A == null) return false;
        if(A.val != B.val) return false;
        return helper(A.left,B.left) && helper(A.right, B.right);
    }
}
